import DayJs from 'dayjs'

export const isOverdue = (flag) => {
  return ['1', '2', '3', '4', '5', '6', '7', 'B'].includes(`${flag}`)
}

export const isMoreStrictOverdue = (flag, n) => {
  return flag * 1 >= n *1 || flag === 'B'
}

export const calcIsCurrentlyOverdue = (list) => {
  const data = []
  list.forEach(item => {
    data.push(item.PD01CD01, item.PD01BD01)
  })
  return data.includes('逾期') ? '1' : '0'
}

export const calcOverdueCount = (list, startMonth, endMonth) => {
  let count = 0
  const normalizedData = {}
  list.forEach(item => {
    if (!normalizedData[item.PDAID]) {
      normalizedData[item.PDAID] = []
    }
    normalizedData[item.PDAID].push(item)
  })

  Object.keys(normalizedData).forEach(key => {
    normalizedData[key] = normalizedData[key].sort(
      (a, b) => DayJs(a.PD01ER03).isBefore(DayJs(b.PD01ER03)) ? 1 : -1
    )
    const currentMonth = DayJs().format('YYYY.MM.01')
    const firstMonth = (normalizedData[key][0] || {}).PD01ER03 || ''
    const isCurrentMonthPeriod = currentMonth === firstMonth
    if (isCurrentMonthPeriod) {
      normalizedData[key] = normalizedData[key].filter(item => {
        const itemDate = DayJs(item.PD01ER03)
        const startDate = DayJs(DayJs().add(-startMonth, 'month').format('YYYY.MM.01'))
        const endDate = DayJs(DayJs().add(-endMonth + 1, 'month').format('YYYY.MM.01'))
        return (itemDate.isSame(startDate) || itemDate.isBefore(startDate)) 
          && (itemDate.isSame(endDate) || itemDate.isAfter(endDate))
      })
    } else {
      normalizedData[key] = normalizedData[key].filter(item => {
        const itemDate = DayJs(item.PD01ER03)
        const startDate = DayJs(DayJs().add(-startMonth - 1, 'month').format('YYYY.MM.01'))
        const endDate = DayJs(DayJs().add(-endMonth, 'month').format('YYYY.MM.01'))
        return (itemDate.isSame(startDate) || itemDate.isBefore(startDate)) 
          && (itemDate.isSame(endDate) || itemDate.isAfter(endDate))
      })
    }
    count += normalizedData[key].filter(item => isOverdue(item.PD01ED01)).length
  })
  return count
}

export const calcHouseLoanCount = (list, startMonth, endMonth) => {
  const ret = {
    maxhouseLoanRepayMonth: 0,
    maxhouseLoanOverdueMonth: 0
  }
  const normalizedData = {}
  list.filter(item => item.PD01AD03.includes('房')).forEach(item => {
    if (!normalizedData[item.PDAID]) {
      normalizedData[item.PDAID] = []
    }
    normalizedData[item.PDAID].push(item)
  })
  Object.keys(normalizedData).forEach(key => {
    normalizedData[key] = normalizedData[key].sort(
      (a, b) => DayJs(a.PD01ER03).isBefore(DayJs(b.PD01ER03)) ? 1 : -1
    )
    const currentMonth = DayJs().format('YYYY.MM.01')
    const firstMonth = (normalizedData[key][0] || {}).PD01ER03 || ''
    const isCurrentMonthPeriod = currentMonth === firstMonth
    if (isCurrentMonthPeriod) {
      normalizedData[key] = normalizedData[key].filter(item => {
        const itemDate = DayJs(item.PD01ER03)
        const startDate = DayJs(DayJs().add(-startMonth, 'month').format('YYYY.MM.01'))
        const endDate = DayJs(DayJs().add(-endMonth + 1, 'month').format('YYYY.MM.01'))
        return (itemDate.isSame(startDate) || itemDate.isBefore(startDate)) 
          && (itemDate.isSame(endDate) || itemDate.isAfter(endDate))
      })
    } else {
      normalizedData[key] = normalizedData[key].filter(item => {
        const itemDate = DayJs(item.PD01ER03)
        const startDate = DayJs(DayJs().add(-startMonth - 1, 'month').format('YYYY.MM.01'))
        const endDate = DayJs(DayJs().add(-endMonth, 'month').format('YYYY.MM.01'))
        return (itemDate.isSame(startDate) || itemDate.isBefore(startDate)) 
          && (itemDate.isSame(endDate) || itemDate.isAfter(endDate))
      })
    }
    const houseLoanRepayMonth = normalizedData[key].length
    const houseLoanOverdueMonth = normalizedData[key].filter(item => isOverdue(item.PD01ED01)).length
    if (houseLoanRepayMonth > ret.maxhouseLoanRepayMonth) {
      ret.maxhouseLoanRepayMonth = houseLoanRepayMonth
    }
    if (houseLoanOverdueMonth > ret.maxhouseLoanOverdueMonth) {
      ret.maxhouseLoanOverdueMonth = houseLoanOverdueMonth
    }
  })
  return ret
}

export const calcMaxContinuousOverdueCount = (list, startMonth, endMonth) => {
  let max = 0
  const normalizedData = {}
  list.forEach(item => {
    if (!normalizedData[item.PDAID]) {
      normalizedData[item.PDAID] = []
    }
    normalizedData[item.PDAID].push(item)
  })
  Object.keys(normalizedData).forEach(key => {
    normalizedData[key] = normalizedData[key].sort(
      (a, b) => DayJs(a.PD01ER03).isBefore(DayJs(b.PD01ER03)) ? 1 : -1
    )

    const currentMonth = DayJs().format('YYYY.MM.01')
    const firstMonth = (normalizedData[key][0] || {}).PD01ER03 || ''
    const isCurrentMonthPeriod = currentMonth === firstMonth
    if (isCurrentMonthPeriod) {
      normalizedData[key] = normalizedData[key].filter(item => {
        const itemDate = DayJs(item.PD01ER03)
        const startDate = DayJs(DayJs().add(-startMonth, 'month').format('YYYY.MM.01'))
        const endDate = DayJs(DayJs().add(-endMonth + 1, 'month').format('YYYY.MM.01'))
        return (itemDate.isSame(startDate) || itemDate.isBefore(startDate)) 
          && (itemDate.isSame(endDate) || itemDate.isAfter(endDate))
      })
    } else {
      normalizedData[key] = normalizedData[key].filter(item => {
        const itemDate = DayJs(item.PD01ER03)
        const startDate = DayJs(DayJs().add(-startMonth - 1, 'month').format('YYYY.MM.01'))
        const endDate = DayJs(DayJs().add(-endMonth, 'month').format('YYYY.MM.01'))
        return (itemDate.isSame(startDate) || itemDate.isBefore(startDate)) 
          && (itemDate.isSame(endDate) || itemDate.isAfter(endDate))
      })
    }

    let count = 0
    for (let i = 0; i < normalizedData[key].length; i++) {
      const iFlag = normalizedData[key][i].PD01ED01
      if (isOverdue(iFlag)) {
        count = 1
        for (let j = i + 1; j < normalizedData[key].length; j++) {
          const jFlag = normalizedData[key][j].PD01ED01
          if (isOverdue(jFlag)) {
            count++
          } else {
            i = j - 1
            break
          }
        }
        if (count > max) {
          max = count
        }
      }
    }
  })
  return max
}

export const calcMnCount = (list, startMonth, endMonth, n) => {
  let count = 0
  const normalizedData = {}
  list.forEach(item => {
    if (!normalizedData[item.PDAID]) {
      normalizedData[item.PDAID] = []
    }
    normalizedData[item.PDAID].push(item)
  })

  Object.keys(normalizedData).forEach(key => {
    normalizedData[key] = normalizedData[key].sort(
      (a, b) => DayJs(a.PD01ER03).isBefore(DayJs(b.PD01ER03)) ? 1 : -1
    )
    const currentMonth = DayJs().format('YYYY.MM.01')
    const firstMonth = (normalizedData[key][0] || {}).PD01ER03 || ''
    const isCurrentMonthPeriod = currentMonth === firstMonth
    if (isCurrentMonthPeriod) {
      normalizedData[key] = normalizedData[key].filter(item => {
        const itemDate = DayJs(item.PD01ER03)
        const startDate = DayJs(DayJs().add(-startMonth, 'month').format('YYYY.MM.01'))
        const endDate = DayJs(DayJs().add(-endMonth + 1, 'month').format('YYYY.MM.01'))
        return (itemDate.isSame(startDate) || itemDate.isBefore(startDate)) 
          && (itemDate.isSame(endDate) || itemDate.isAfter(endDate))
      })
    } else {
      normalizedData[key] = normalizedData[key].filter(item => {
        const itemDate = DayJs(item.PD01ER03)
        const startDate = DayJs(DayJs().add(-startMonth - 1, 'month').format('YYYY.MM.01'))
        const endDate = DayJs(DayJs().add(-endMonth, 'month').format('YYYY.MM.01'))
        return (itemDate.isSame(startDate) || itemDate.isBefore(startDate)) 
          && (itemDate.isSame(endDate) || itemDate.isAfter(endDate))
      })
    }
    count += normalizedData[key].filter(item => isMoreStrictOverdue(item.PD01ED01, n)).length
  })
  return count
}

export const calcQueryCount = (
  list,
  endDate,
  { reasons = ['贷款审批', '信用卡审批', '担保资格审查'], type = '' } = {}
) => {
  list = list.filter(
    item => reasons.includes(item.PH010Q03)
  ).filter(
    item => DayJs(item.PH010R01).isAfter(endDate)
  )
  if (type) {
    list = list.map(item => `${item.PH010Q02}${item.PH010Q03}`)
    return new Set(list).size
  }
  return list.length
}

export const normalizeCreditInfo = (creditDetail) => {
  const creditInfo = {}

  const pcPcoPchObj = creditDetail.pc_pco_pch_obj || {}
  const originPcCrTrList = creditDetail.pc_cr_tr_list || []
  const pcPdaPdBList = creditDetail.pc_pda_pd_b_list || []
  const pcPdaPdCList = creditDetail.pc_pda_pd_c_list || []
  const pcPdaPdEList = (creditDetail.pc_pda_pd_e_list || []).map(item => {
    const pcCrTrItem = originPcCrTrList.find(i => i.PDAID === item.PDAID) || {}
    return {
      ...pcCrTrItem,
      ...item
    }
  })
  const pcPqoPcList = creditDetail.pc_pqo_pc_list || []
  const pcPqoPhList = creditDetail.pc_pqo_ph_list || []
  const pcPcoPcaList = creditDetail.pc_pco_pca_list || []
  const pcPubInfoObj = creditDetail.pc_pub_info_obj || {}
  const pcPcoPceObj = creditDetail.pc_pco_pce_obj || {}
  const pcPcoPcfObj = creditDetail.pc_pco_pcf_obj || {}
  const pcPcoPcgObj = creditDetail.pc_pco_pcg_obj || {}

  const normalizedPcCrTrList = originPcCrTrList.map(item => {
    let pdBItem = {}
    if (pcPdaPdBList.length) {
      pdBItem = pcPdaPdBList.find(i => item.ID === i['PDAID']) || {}
    }

    let pdCItem = {}
    if (pcPdaPdCList.length) {
      pdCItem = pcPdaPdCList.find(i => item.ID === i['PDAID']) || {}
    }

    let pdEList = []
    if (pcPdaPdEList.length) {
      pdEList = pcPdaPdEList.filter(i => item.ID === i['PDAID'])
    }
    
    const pdEYears = []
    pdEList.forEach(item => {
      const [year, month] = item.PD01ER03.split('.')
      let yearData = pdEYears.find(i => i.year === year)
      if (!yearData) {
        yearData = {
          year,
          data: []
        }
        pdEYears.push(yearData)
      }
      yearData.data.push({
        month: Number(month),
        PD01ED01: item.PD01ED01,
        PD01EJ01: item.PD01EJ01
      })
    })
    pdEYears.sort((a, b) => b.year - a.year)
    pdEYears.forEach(item => {
      item.data.sort((a, b) => a.month - b.month)
    })
    let minDateStr = ''
    let maxDateStr = ''
    
    if (pdEYears[pdEYears.length - 1] && pdEYears[pdEYears.length - 1].data) {
      const minDate = pdEYears[pdEYears.length - 1].data[0]
      minDateStr = `${pdEYears[pdEYears.length - 1].year}年${minDate.month}月`
    }
    if (pdEYears[0] && pdEYears[0].data) {
      const maxDate = pdEYears[0].data[pdEYears[0].data.length - 1]
      maxDateStr = `${pdEYears[0].year}年${maxDate.month}月`
    }

    return {
      ...item,
      ...({
        ...pdBItem,
        PD01BR03: minDateStr
      }),
      ...({
        ...pdCItem,
        PD01CR04: maxDateStr
      }),
      minDate: minDateStr,
      maxDate: maxDateStr,
      pdEYears: pdEYears.map(item => {
        const mapping = {}
        item.data.forEach(i => {
          mapping[i.month] = {
            ...i
          }
        })
        return {
          ...item,
          data: mapping
        }
      })
    }
  }).filter(
    item => !['结清', '销户'].includes(item.PD01BD01)
  )

  const creditCardPcCrTrList = normalizedPcCrTrList.filter(
    item => ['R2', 'R3'].includes(item.PD01AD01)
  )

  // 是否已查征信
  creditInfo.hasCheckCredit = '1'

  // 信用卡
  const totalCreditCardCreditAmount = pcPcoPchObj.PC02HJ01 || 0
  const creditCardUsedLimit = pcPcoPchObj.PC02HJ04 || 0
  creditInfo.totalCreditCardCreditAmount = totalCreditCardCreditAmount
  creditInfo.maxSingleCreditCardLimit = pcPcoPchObj.PC02HJ02 || 0
  creditInfo.creditCardUsedLimit = creditCardUsedLimit
  const creditCardUsageRate = totalCreditCardCreditAmount ? creditCardUsedLimit * 100 / totalCreditCardCreditAmount : 0
  creditInfo.creditCardUsageRate = Number.isFinite(creditCardUsageRate) ? creditCardUsageRate.toFixed(0) : 999
  creditInfo.averageUsageLimit6Months = pcPcoPchObj.PC02HJ05 || 0
  creditInfo.creditCardInsCount = pcPcoPchObj.PC02HS01 || 0
  creditInfo.creditCardAccountCount = pcPcoPchObj.PC02HS02 || 0
  
  const pcCrTrList = normalizedPcCrTrList.filter(
    item => ['D1', 'R1', 'R4'].includes(item.PD01AD01)
  )

  // 贷款
  const nonBankLoanList = pcCrTrList.filter(item => !['商业银行'].includes(item.PD01AD02))
  // 贷款余额
  creditInfo.totalLoanRestAmount = 0
  // 贷款机构数
  creditInfo.loanInsCount = new Set(pcCrTrList.map(item => item.PD01AI04)).size
  // 贷款账户数
  creditInfo.loanAccountCount = pcCrTrList.length
  // 贷款非银机构数
  creditInfo.loanNonBankInsCount = new Set(nonBankLoanList.map(item => item.PD01AI04)).size
  // 贷款非银账户数
  creditInfo.loanNonBankAccountCount = nonBankLoanList.length
  pcCrTrList.forEach(item => {
    creditInfo.totalLoanRestAmount += Number(item.PD01CJ01 || 0)
  })
  // 处理大额分期卡
  creditCardPcCrTrList.forEach(item => {
    if (Number(item.PD01CJ03 || 0) > 0) {
      creditInfo.totalLoanRestAmount += Number(item.PD01CJ01 || 0)
    }
  })
  // 贷款近6个月均还款
  creditInfo.loanAverageMonthRepayAmount =
    Number(pcPcoPceObj.PC02EJ03 || 0) +
    Number(pcPcoPcfObj.PC02FJ03 || 0) +
    Number(pcPcoPcgObj.PC02GJ03 || 0)

  // 信用贷 除 ['个人住房商业贷款', '个人商用房（含商住两用）贷款', '个人住房公积金贷款', '个人汽车消费贷款']
  const creditLoanList = pcCrTrList.filter(
    item => !item.PD01AD03.includes('房') && !item.PD01AD03.includes('车')
  )
  const nonBankCreditLoanList = creditLoanList.filter(item => !['商业银行'].includes(item.PD01AD02))
  // 信用贷款余额
  creditInfo.totalCreditLoanRestAmount = 0
  // 信用贷款机构数
  creditInfo.creditLoanInsCount = new Set(creditLoanList.map(item => item.PD01AI04)).size
  // 信用贷款账户数
  creditInfo.creditLoanAccountCount = creditLoanList.length
  // 信用贷款非银机构数
  creditInfo.creditLoanNonBankInsCount = new Set(nonBankCreditLoanList.map(item => item.PD01AI04)).size
  // 信用贷款非银账户数
  creditInfo.creditLoanNonBankAccountCount = nonBankCreditLoanList.length
  creditLoanList.forEach(item => {
    creditInfo.totalCreditLoanRestAmount += Number(item.PD01CJ01 || 0)
  })
  // 处理大额分期卡
  creditCardPcCrTrList.forEach(item => {
    if (Number(item.PD01CJ03 || 0) > 0) {
      creditInfo.totalCreditLoanRestAmount += Number(item.PD01CJ01 || 0)
    }
  })
  // 信用贷款近6个月均还款
  creditInfo.creditLoanAverageMonthRepayAmount =
    Number(pcPcoPceObj.PC02EJ03 || 0) +
    Number(pcPcoPcfObj.PC02FJ03 || 0) +
    Number(pcPcoPcgObj.PC02GJ03 || 0)

  // 非信用贷 房贷 ['个人住房商业贷款', '个人商用房（含商住两用）贷款', '个人住房公积金贷款']
  const houseLoanList = pcCrTrList.filter(
    item =>  item.PD01AD03.includes('房')
  )
  creditInfo.houseLoanCount = houseLoanList.length
  creditInfo.totalHouseLoanAmount = 0
  creditInfo.totalHouseLoanRestAmount = 0
  creditInfo.houseLoanAverageMonthRepayAmount = 0
  houseLoanList.forEach(item => {
    creditInfo.totalHouseLoanAmount += Number(item.PD01AJ01 || 0)
    creditInfo.totalHouseLoanRestAmount += Number(item.PD01CJ01 || 0)
    creditInfo.houseLoanAverageMonthRepayAmount += Number(item.PD01CJ04 || 0)
  })
  const ret6Months = calcHouseLoanCount(pcPdaPdEList, 0, 6, '房')
  const ret2Years= calcHouseLoanCount(pcPdaPdEList, 0, 12, '房')
  const ret5Years = calcHouseLoanCount(pcPdaPdEList, 0, 60, '房')
  const ret2To5Years = calcHouseLoanCount(pcPdaPdEList, 24, 60, '房')
  creditInfo.maxhouseLoanRepayMonth = ret5Years.maxhouseLoanRepayMonth
  creditInfo.maxhouseLoanOverdueCount6Months = ret6Months.maxhouseLoanOverdueMonth
  creditInfo.maxhouseLoanOverdueCount1Year = ret2Years.maxhouseLoanOverdueMonth
  creditInfo.maxhouseLoanOverdueCount2To5Years = ret2To5Years.maxhouseLoanOverdueMonth
  creditInfo.maxhouseLoanOverdueCount5Years = ret5Years.maxhouseLoanOverdueMonth

  // 信用贷款近6个月均还款
  creditInfo.creditLoanAverageMonthRepayAmount -= creditInfo.houseLoanAverageMonthRepayAmount

  // 非信用贷 车贷 ['个人汽车消费贷款']
  const carLoanList = pcCrTrList.filter(
    item => item.PD01AD03.includes('车')
  )
  creditInfo.carLoanCount = carLoanList.length
  creditInfo.totalCarLoanAmount = 0
  creditInfo.totalCarLoanRestAmount = 0
  creditInfo.carLoanAverageMonthRepayAmount = 0
  carLoanList.forEach(item => {
    creditInfo.totalCarLoanAmount += Number(item.PD01AJ01 || 0)
    creditInfo.totalCarLoanRestAmount += Number(item.PD01CJ01 || 0)
    creditInfo.carLoanAverageMonthRepayAmount += Number(item.PD01CJ04 || 0)
  })
  // 非信用贷 经营性贷款 ['个人经营性贷款']
  const businessLoanList = pcCrTrList.filter(
    item => item.PD01AD03 === '个人经营性贷款'
  )
  creditInfo.businessLoanCount = businessLoanList.length
  // 非信用贷 担保类贷款数 ['个人经营性贷款']
  const securedLoanList = pcCrTrList.filter(
    item => item.PD01AD03.includes('担保')
  )
  creditInfo.securedLoanCount = securedLoanList.length

  // 逾期
  creditInfo.isCurrentlyOverdue = calcIsCurrentlyOverdue(pcCrTrList)
  creditInfo.overdueCount3Months = calcOverdueCount(pcPdaPdEList, 0, 3)
  creditInfo.overdueCount6Months = calcOverdueCount(pcPdaPdEList, 0, 6)
  creditInfo.overdueCount1Year = calcOverdueCount(pcPdaPdEList, 0, 12)
  creditInfo.overdueCount2Years = calcOverdueCount(pcPdaPdEList, 0, 24)
  creditInfo.overdueCount2To5Years = calcOverdueCount(pcPdaPdEList, 24, 60)
  creditInfo.overdueCount5Years = calcOverdueCount(pcPdaPdEList, 0, 60)
  creditInfo.maxContinuousOverdueCount2Years = calcMaxContinuousOverdueCount(pcPdaPdEList, 0, 24)
  creditInfo.maxContinuousOverdueCount5Years = calcMaxContinuousOverdueCount(pcPdaPdEList, 0, 60)
  creditInfo.M1Count6Months = calcMnCount(pcPdaPdEList, 0, 6, '1')
  creditInfo.M1Count1Year = calcMnCount(pcPdaPdEList, 0, 12, '1')
  creditInfo.M1Count2Years = calcMnCount(pcPdaPdEList, 0, 24, '1')
  creditInfo.M1Count2To5Years = calcMnCount(pcPdaPdEList, 24, 60, '1')
  creditInfo.M2Count6Months = calcMnCount(pcPdaPdEList, 0, 6, '2')
  creditInfo.M2Count1Year = calcMnCount(pcPdaPdEList, 0, 12, '2')
  creditInfo.M2Count2Years = calcMnCount(pcPdaPdEList, 0, 24, '2')
  creditInfo.M2Count2To5Years = calcMnCount(pcPdaPdEList, 24, 60, '2')
  creditInfo.M3Count6Months = calcMnCount(pcPdaPdEList, 0, 6, '3')
  creditInfo.M3Count1Year = calcMnCount(pcPdaPdEList, 0, 12, '3')
  creditInfo.M3Count2Years = calcMnCount(pcPdaPdEList, 0, 24, '3')
  creditInfo.M3Count2To5Years = calcMnCount(pcPdaPdEList, 24, 60, '3')
  creditInfo.M4Count6Months = calcMnCount(pcPdaPdEList, 0, 6, '4')
  creditInfo.M4Count1Year = calcMnCount(pcPdaPdEList, 0, 12, '4')
  creditInfo.M4Count2Years = calcMnCount(pcPdaPdEList, 0, 24, '4')
  creditInfo.M4Count2To5Years = calcMnCount(pcPdaPdEList, 24, 60, '4')
  creditInfo.M5Count6Months = calcMnCount(pcPdaPdEList, 0, 6, '5')
  creditInfo.M5Count1Year = calcMnCount(pcPdaPdEList, 0, 12, '5')
  creditInfo.M5Count2Years = calcMnCount(pcPdaPdEList, 0, 24, '5')
  creditInfo.M5Count2To5Years = calcMnCount(pcPdaPdEList, 24, 60, '5')
  creditInfo.M6Count6Months = calcMnCount(pcPdaPdEList, 0, 6, '6')
  creditInfo.M6Count1Year = calcMnCount(pcPdaPdEList, 0, 12, '6')
  creditInfo.M6Count2Years = calcMnCount(pcPdaPdEList, 0, 24, '6')
  creditInfo.M6Count2To5Years = calcMnCount(pcPdaPdEList, 24, 60, '6')
  creditInfo.M7Count6Months = calcMnCount(pcPdaPdEList, 0, 6, '7')
  creditInfo.M7Count1Year = calcMnCount(pcPdaPdEList, 0, 12, '7')
  creditInfo.M7Count2Years = calcMnCount(pcPdaPdEList, 0, 24, '7')
  creditInfo.M7Count2To5Years = calcMnCount(pcPdaPdEList, 24, 60, '7')

  // 查询
  creditInfo.myQueryCount1Month = (pcPqoPcList[0] || {}).PC05BS05 || 0
  creditInfo.queryCountToday = calcQueryCount(pcPqoPhList, DayJs().add(-1, 'day'))
  creditInfo.queryCount1Month = calcQueryCount(pcPqoPhList, DayJs().add(-1, 'month'))
  creditInfo.queryCount2Months = calcQueryCount(pcPqoPhList, DayJs().add(-2, 'month'))
  creditInfo.queryCount3Months = calcQueryCount(pcPqoPhList, DayJs().add(-3, 'month'))
  creditInfo.queryCount6Months = calcQueryCount(pcPqoPhList, DayJs().add(-6, 'month'))
  creditInfo.queryCount1Year = calcQueryCount(pcPqoPhList, DayJs().add(-12, 'month'))

  // 查询机构
  creditInfo.orgQueryCountToday = calcQueryCount(pcPqoPhList, DayJs().add(-1, 'day'), { type: 'org' })
  creditInfo.orgQueryCount1Month = calcQueryCount(pcPqoPhList, DayJs().add(-1, 'month'), { type: 'org' })
  creditInfo.orgQueryCount2Months = calcQueryCount(pcPqoPhList, DayJs().add(-2, 'month'), { type: 'org' })
  creditInfo.orgQueryCount3Months = calcQueryCount(pcPqoPhList, DayJs().add(-3, 'month'), { type: 'org' })
  creditInfo.orgQueryCount6Months = calcQueryCount(pcPqoPhList, DayJs().add(-6, 'month'), { type: 'org' })
  creditInfo.orgQueryCount1Year = calcQueryCount(pcPqoPhList, DayJs().add(-12, 'month'), { type: 'org' })

  // 贷款审批查询
  creditInfo.loanQueryCountToday = calcQueryCount(pcPqoPhList, DayJs().add(-1, 'day'), { reasons: ['贷款审批'] })
  creditInfo.loanQueryCount1Month = calcQueryCount(pcPqoPhList, DayJs().add(-1, 'month'), { reasons: ['贷款审批'] })
  creditInfo.loanQueryCount2Months = calcQueryCount(pcPqoPhList, DayJs().add(-2, 'month'), { reasons: ['贷款审批'] })
  creditInfo.loanQueryCount3Months = calcQueryCount(pcPqoPhList, DayJs().add(-3, 'month'), { reasons: ['贷款审批'] })
  creditInfo.loanQueryCount6Months = calcQueryCount(pcPqoPhList, DayJs().add(-6, 'month'), { reasons: ['贷款审批'] })
  creditInfo.loanQueryCount1Year = calcQueryCount(pcPqoPhList, DayJs().add(-12, 'month'), { reasons: ['贷款审批'] })

  // 贷款审批机构
  creditInfo.loanOrgQueryCountToday = calcQueryCount(pcPqoPhList, DayJs().add(-1, 'day'), { reasons: ['贷款审批'], type: 'org' })
  creditInfo.loanOrgQueryCount1Month = calcQueryCount(pcPqoPhList, DayJs().add(-1, 'month'), { reasons: ['贷款审批'], type: 'org' })
  creditInfo.loanOrgQueryCount2Months = calcQueryCount(pcPqoPhList, DayJs().add(-2, 'month'), { reasons: ['贷款审批'], type: 'org' })
  creditInfo.loanOrgQueryCount3Months = calcQueryCount(pcPqoPhList, DayJs().add(-3, 'month'), { reasons: ['贷款审批'], type: 'org' })
  creditInfo.loanOrgQueryCount6Months = calcQueryCount(pcPqoPhList, DayJs().add(-6, 'month'), { reasons: ['贷款审批'], type: 'org' })
  creditInfo.loanOrgQueryCount1Year = calcQueryCount(pcPqoPhList, DayJs().add(-12, 'month'), { reasons: ['贷款审批'], type: 'org' })

  // 信用卡审批查询
  creditInfo.creditCardQueryCountToday = calcQueryCount(pcPqoPhList, DayJs().add(-1, 'day'), { reasons: ['信用卡审批'] })
  creditInfo.creditCardQueryCount1Month = calcQueryCount(pcPqoPhList, DayJs().add(-1, 'month'), { reasons: ['信用卡审批'] })
  creditInfo.creditCardQueryCount2Months = calcQueryCount(pcPqoPhList, DayJs().add(-2, 'month'), { reasons: ['信用卡审批'] })
  creditInfo.creditCardQueryCount3Months = calcQueryCount(pcPqoPhList, DayJs().add(-3, 'month'), { reasons: ['信用卡审批'] })
  creditInfo.creditCardQueryCount6Months = calcQueryCount(pcPqoPhList, DayJs().add(-6, 'month'), { reasons: ['信用卡审批'] })
  creditInfo.creditCardQueryCount1Year = calcQueryCount(pcPqoPhList, DayJs().add(-12, 'month'), { reasons: ['信用卡审批'] })

  // 信用卡审批机构
  creditInfo.creditCardOrgQueryCountToday = calcQueryCount(pcPqoPhList, DayJs().add(-1, 'day'), { reasons: ['信用卡审批'], type: 'org' })
  creditInfo.creditCardOrgQueryCount1Month = calcQueryCount(pcPqoPhList, DayJs().add(-1, 'month'), { reasons: ['信用卡审批'], type: 'org' })
  creditInfo.creditCardOrgQueryCount2Months = calcQueryCount(pcPqoPhList, DayJs().add(-2, 'month'), { reasons: ['信用卡审批'], type: 'org' })
  creditInfo.creditCardOrgQueryCount3Months = calcQueryCount(pcPqoPhList, DayJs().add(-3, 'month'), { reasons: ['信用卡审批'], type: 'org' })
  creditInfo.creditCardOrgQueryCount6Months = calcQueryCount(pcPqoPhList, DayJs().add(-6, 'month'), { reasons: ['信用卡审批'], type: 'org' })
  creditInfo.creditCardOrgQueryCount1Year = calcQueryCount(pcPqoPhList, DayJs().add(-12, 'month'), { reasons: ['信用卡审批'], type: 'org' })
  
  // 其他
  creditInfo.isCreditClassifiedToFiveLevels = pcCrTrList.filter(
    item => item.PD01CD02 && item.PD01CD02 !== '正常' && item.PD01CD02 !== '未分类'
  ).length ? '1' : '0'
  creditInfo.isCreditWhiteUser = pcPcoPcaList.reduce((pre, cur) => pre + Number(cur.PC02AS03 || 0), 0) === 0 ? '1' : '0'
  creditInfo.isInvolvedInLitigation = (pcPubInfoObj.PCJ || []).length ? '1' : '0'

  return creditInfo
}